from setux.logger import error, debug
from setux.core.package import SystemPackager


# pylint: disable=expression-not-assigned


class Void(SystemPackager):
    manager = 'xbps'

    def do_update(self):
        self.run(
            'xbps-install -Sy'
        )
        self.run(
            'xbps-install -uy xbps'
        )

    def do_upgrade(self):
        self.run(
            'xbps-install -uy'
        )

    def do_init(self):
        self.do_update()

    def do_installed(self):
        ret, out, err = self.run(
            'xbps-query -m',
        report = 'quiet')
        for line in out:
            name, _, ver = line.rpartition('-')
            yield name, ver

    def do_bigs(self):
        #__to__do__
        ret, out, err = self.run(
            '',
        report = 'quiet')
        yield from out

    def do_upgradable(self):
        #__to__do__
        ret, out, err = self.run(
            '',
        report = 'quiet')
        for line in out:
            try:
                name, ver = line.split(' ', 1)
                yield name, ver
            except Exception as x:
                debug(line)

    def do_available(self):
        #__to__do__
        ret, out, err = self.run(
        report = 'quiet')
        for line in out:
            print(line)
            name, ver = line.split()
            yield name, ver

    def do_remove(self, pkg):
        self.run(
            f'xbps-remove -Rfy {pkg}'
        )

    def do_cleanup(self):
        self.run(
            'xbps-remove -oOy'
        )

    def do_install(self, pkg, ver=None):
        ret, out, err = self.run(
            f'xbps-install -y {pkg}'
        )
        if err:
            msg = '\n'.join(err)
            debug(msg) if ret==0 else error(msg)

